package com.siysoft.tc.admin.action;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siysoft.tc.admin.service.IAdminInterestService;
import com.siysoft.tc.base.bean.property.ResourceType;
import com.siysoft.tc.base.bean.to.Resource;
import com.siysoft.tc.base.service.IResourceService;
import com.siysoft.util.IMap;
import com.siysoft.util.fileupload.FileUpload;

/**
 * <p>Title: AdminInterestController</p>
 * <p>Description:咨询管理功能</p>
 * <p>Company: dmpb</p>
 * @author    victor
 * @date       2015年11月12日
 */
@Controller
@RequestMapping(value = "/admin/interest")
public class AdminInterestController {
	@Autowired
	IAdminInterestService interestService;
	@Autowired
	IResourceService resourceService;
	
	//兴趣管理
	@RequestMapping(value = "/toInterest")
	public String toInterest(HttpServletRequest request) throws Exception{
		IMap iMap = new IMap(request);
		List<Map<String,Object>> seekInterests = interestService.seekInterests(iMap);
		request.setAttribute("interestList", seekInterests);
		return "/admin/interest/interestList";
	}
	//跳转添加兴趣页面
	@RequestMapping(value = "/addInterest")
	public String addInterest(HttpServletRequest request) throws Exception{
		IMap iMap = new IMap(request);
		Integer id = interestService.addInterests(iMap);
		
		///保存兴趣头像
		Resource resource;
		String local = request.getServletContext().getRealPath("/"); 
		String fixedAvatar = request.getParameter("head");
		if(fixedAvatar != null){
			resource=new Resource();
			resource.setDataType(ResourceType.INTEREST);
			resource.setImgPath(new FileUpload().uploadFileByBase64(fixedAvatar, String.valueOf(id), request));
			resource.setDataId(id);
			resource.setAuditFlag((byte)0); 
			resource.setUploadTime(new Timestamp(System.currentTimeMillis()));
			this.resourceService.changeResource(resource, local);
		}
		
		return "redirect:/admin/interest/toInterest.htm";
	}
	
	//跳转编辑兴趣页面
	@RequestMapping(value = "/toEditInterest")
	public String toEditInterest(HttpServletRequest request) throws Exception{
		IMap iMap = new IMap(request);
		List<Map<String,Object>> seekInterests = interestService.seekInterests(iMap);
		request.setAttribute("interest", seekInterests.get(0));
		return "/admin/interest/editInterest";
	}
	
	//删除兴趣
	@RequestMapping(value = "/delInterest")
	public String delInterest(HttpServletRequest request) throws Exception{
		IMap iMap = new IMap(request);
		interestService.delInterest(iMap.getInt("id"));
		return "redirect:/admin/interest/toInterest.htm";
	}
	
	//跳转添加系统消息页面
	@RequestMapping(value = "/toAddInterest")
	public String toAddInterest(HttpServletRequest request) throws Exception{
		return "/admin/interest/addInterest";
	}
	
	
	//编辑兴趣
	@RequestMapping(value = "/editInterest")
	public String EditInterest(HttpServletRequest request) throws Exception{
		
		String resourceId = request.getParameter("resource_id");
		///保存兴趣头像
		Resource resource;
		String local = request.getServletContext().getRealPath("/"); 
		String fixedAvatar = request.getParameter("head");
		if(fixedAvatar != null){
			resource=new Resource();
			resource.setDataType(ResourceType.INTEREST);
			resource.setImgPath(new FileUpload().uploadFileByBase64(fixedAvatar, resourceId, request));
			resource.setDataId(Integer.parseInt(resourceId));
			resource.setAuditFlag((byte)0); 
			resource.setUploadTime(new Timestamp(System.currentTimeMillis()));
			this.resourceService.changeResource(resource, local);
		}
			
		return "forward:/admin/interest/toInterest.htm";
	}
	

	
	
}
